<?php
class AjaxController extends Controller {
    public $viewClass = 'Smarty';
    public $uses = array('ShipperModel', 'AreaModel', 
    					'ShippingFeeModel', 'KanriModel',
						'ShipperDiffModel', 'ProductDiffModel');
	
	// kanri change
    public function kanriChange() {
    	$this->autoRender = false;
		$this->layout = false;
		$arrReturn = array();
		$arrReturn['mode'] = 'update';
		$arrReturn['status'] = '1';
		$arrReturn['id'] = 0;
		if ($this->request->is('ajax')) {
			$id = $this->request->data['id'];
			$val = $this->request->data['value'];
			$field = $this->request->data['field'];
			// check validate cd with field TODO
			if (trim($val) == '') {
				$arrReturn['status'] = '0';
			} else {
				// save 
				$this->KanriModel->id = $id;
				$chk = $this->KanriModel->save(array($field => $val));
				$arrReturn['status'] = $chk == false ? '0' : '1';
				$arrReturn['id'] = $this->KanriModel->id;
			}
		}
		echo json_encode($arrReturn);
		exit();
    }
		
	// change shipper cd with id input
    public function shipperChange() {
    	$this->autoRender = false;
		$this->layout = false;
		$arrReturn = array();
		$arrReturn['mode'] = 'update';
		$arrReturn['status'] = '1';
		$arrReturn['id'] = 0;
		if ($this->request->is('ajax')) {
			$id = $this->request->data['id'];
			$val = $this->request->data['value'];
			$field = $this->request->data['field'];
			// check validate cd with field TODO
			if (trim($val) == '') {
				$arrReturn['status'] = '0';
			} else {
				// save 
				if ($id != 'insert') {
					$this->ShipperModel->id = $id;
				} else {
					$arrReturn['mode'] = 'insert';
				}
				$chk = $this->ShipperModel->save(array($field => $val));
				$arrReturn['status'] = $chk == false ? '0' : '1';
				$arrReturn['id'] = $this->ShipperModel->id;
			}
		}
		echo json_encode($arrReturn);
		exit();
    }
	// product diff change
    public function productChange() {
    	$this->autoRender = false;
		$this->layout = false;
		$arrReturn = array();
		$arrReturn['mode'] = 'update';
		$arrReturn['status'] = '1';
		$arrReturn['id'] = 0;
		if ($this->request->is('ajax')) {
			$id = $this->request->data['id'];
			$val = $this->request->data['value'];
			$field = $this->request->data['field'];
			// check validate cd with field TODO
			if (trim($val) == '') {
				$arrReturn['status'] = '0';
			} else {
				// save 
				if ($id != 'insert') {
					$this->ProductDiffModel->id = $id;
				} else {
					$arrReturn['mode'] = 'insert';
				}
				$chk = $this->ProductDiffModel->save(array($field => $val));
				$arrReturn['status'] = $chk == false ? '0' : '1';
				$arrReturn['id'] = $this->ProductDiffModel->id;
			}
		}
		echo json_encode($arrReturn);
		exit();
    }	
	
	// Shipper diff change 
    public function shipperDiffChange() {
    	$this->autoRender = false;
		$this->layout = false;
		$arrReturn = array();
		$arrReturn['mode'] = 'update';
		$arrReturn['status'] = '1';
		$arrReturn['id'] = 0;
		if ($this->request->is('ajax')) {
			$id = $this->request->data['id'];
			$val = $this->request->data['value'];
			$field = $this->request->data['field'];
			// check validate cd with field TODO
			if (trim($val) == '') {
				$arrReturn['status'] = '0';
			} else {
				// save 
				if ($id != 'insert') {
					$this->ShipperDiffModel->id = $id;
				} else {
					$arrReturn['mode'] = 'insert';
				}
				$chk = $this->ShipperDiffModel->save(array($field => $val));
				$arrReturn['status'] = $chk == false ? '0' : '1';
				$arrReturn['id'] = $this->ShipperDiffModel->id;
			}
		}
		echo json_encode($arrReturn);
		exit();
    } 	
    public function areaChange() {
    	$this->autoRender = false;
		$this->layout = false;
		$arrReturn = array();
		$arrReturn['mode'] = 'update';
		$arrReturn['status'] = '1';
		$arrReturn['id'] = 0;
		if ($this->request->is('ajax')) {
			$id = $this->request->data['id'];
			$val = $this->request->data['value'];
			$field = $this->request->data['field'];
			// check validate cd with field TODO
			if (trim($val) == '') {
				$arrReturn['status'] = '0';
			} else {
				// save 
				if ($id != 'insert') {
					$this->AreaModel->id = $id;
				} else {
					$arrReturn['mode'] = 'insert';
				}
				$chk = $this->AreaModel->save(array($field => $val));
				$arrReturn['status'] = $chk == false ? '0' : '1';
				$arrReturn['id'] = $this->AreaModel->id;
			}
		}
		echo json_encode($arrReturn);
		exit();
    }
	
    public function shippingFeeChange() {
    	$this->autoRender = false;
		$this->layout = false;
		$arrReturn = array();
		$arrReturn['mode'] = 'update';
		$arrReturn['status'] = '1';
		$arrReturn['id'] = 0;
		if ($this->request->is('ajax')) {
			$id = $this->request->data['id'];
			$val = $this->request->data['value'];
			$field = $this->request->data['field'];
			// check validate cd with field TODO
			if (trim($val) == '') {
				$arrReturn['status'] = '0';
			} else {
				// save 
				if ($id != 'insert') {
					$this->ShippingFeeModel->id = $id;
				} else {
					$arrReturn['mode'] = 'insert';
				}
				$chk = $this->ShippingFeeModel->save(array($field => $val));
				$arrReturn['status'] = $chk == false ? '0' : '1';
				$arrReturn['id'] = $this->ShippingFeeModel->id;
			}
		}
		echo json_encode($arrReturn);
		exit();
    }
}
